#include "plugin1.h"

#include "pluginwidget.h"

#include <QDebug>

class Plugin1Private {
public:
  Plugin1Private(Plugin1 *q) : q_ptr(q) {}

private:
  Plugin1 *const q_ptr;
  Q_DECLARE_PUBLIC(Plugin1)

  QWidget *pluginWidget{nullptr};
  QString messgae;
};

static Plugin1 *inst = nullptr;
Plugin1::Plugin1(QObject *parent)
    : QObject(parent), d_ptr(new Plugin1Private(this)) {

  inst = this;
}

Plugin1::~Plugin1() {
  delete d_ptr;
  inst = nullptr;
}

Plugin1 *Plugin1::instance() { return inst; }

QWidget *Plugin1::widget() {
  Q_D(Plugin1);
  if (!d->pluginWidget)
    d->pluginWidget = new PluginWidget;

  return d->pluginWidget;
}

void Plugin1::receiveMessage(PluginMessage message) {
  Q_D(Plugin1);
  qDebug() << Q_FUNC_INFO << " " << message.message;
  d->messgae = message.message;
}

PluginMessage Plugin1::handleMessage(PluginMessage message) {
  Q_D(Plugin1);
  qDebug() << Q_FUNC_INFO << " " << message.message;

  PluginMessage pluginMessage;
  pluginMessage.src = "plugin2";
  pluginMessage.dst = message.src;
  // if (message.message == "getLabel") {
  // pluginMessage.message = d->pluginWidget->getLabel();
  //}

  return pluginMessage;
}

QString Plugin1::getPlugin2Label() {
  Q_D(Plugin1);
  d->messgae.clear();
  emit sendMessage(PluginMessage({"plugin1", "plugin2", "getLabel"}));

  return d->messgae;
}
